// pages/earningsRecord/earningsRecord.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    active:0,
    type:1,
    page:1,
    intiveList:[],
    pinList:[]
  },
  onChange(e){
    var that = this
    var name = e.detail.name;
    var active=parseInt(name) + 1;
    that.setData({
      active:name,
      type:active,
      page:1
    })
    var uid =wx.getStorageSync('uid');
    var param = {
      uid: uid,
      type:active,
      page:1
    }
    app.wxRequest('POST', "Index/incomeList", param, (res) => {
      var data = res.data;
      if(active == 1){
        that.setData({
          intiveList: data
        })
      }else if(active == 2){
        that.setData({
          pinList: data
        })
      }
    }, (err) => {
      console.log(err.errMsg)
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that=this;
    var uid =wx.getStorageSync('uid');
    var type=that.data.type;
    var param = {
      uid: uid,
      type:type,
      page:1
    }
    app.wxRequest('POST', "Index/incomeList", param, (res) => {
      var data = res.data;
      that.setData({
        intiveList: data
      })
    }, (err) => {
      console.log(err.errMsg)
    })
  },

 

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.showNavigationBarLoading();
    var that=this;
    var uid=wx.getStorageSync('uid');
    var type=that.data.type;
    var param = {
      uid: uid,
      type:type,
      page:1
    }
    app.wxRequest('POST', "Index/incomeList", param, (res) => {
      var data = res.data;
      if(type == 1){
        that.setData({
          intiveList: data
        })
      }else if(type == 2){
        that.setData({
          pinList: data
        })
      }
      
    }, (err) => {
      console.log(err.errMsg)
    })
    setTimeout(function(){
      wx.hideNavigationBarLoading();
      wx.stopPullDownRefresh();
    },3000)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that=this;
    var page=that.data.page;
    var newPage=parseInt(page) + 1;
    var uid=wx.getStorageSync('uid');
    var type=that.data.type;
    var intiveList=that.data.intiveList;
    var pinList=that.data.pinList;
    var param={
      uid: uid,
      type:type,
      page:newPage
    };
    app.wxRequest('POST', "Index/incomeList", param, (res) => {
      var data = res.data;
      var code=res.code;
      if(code==1){
        if(type == 1){
          that.setData({
            intiveList: intiveList.concat(data),
            page:newPage
          })
        }else if(type == 2){
          that.setData({
            pinList: pinList.concat(data),
            page:newPage
          })
        }
      }else{
        wx.showToast({
          title: res.msg,
          icon:"none",
          duration:1500
        })
      }
      
    }, (err) => {
      console.log(err.errMsg)
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})